#! /bin/sh
#
### BEGIN INIT INFO
# Provides: captagent
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Required-Start: $local_fs $network
# Required-Stop:
# Short-Description: captagent - the Open Source Homer Capture Agent
# Description: Homer captagent is an Open Source Capture Programm released
# 	under GPLv3, able to handle thousands of call setups per second.
### END INIT INFO

cap=/usr/local/bin/captagent
prog=captagent
pidfile=/var/run/$prog.pid
lockfile=/var/lock/$prog
RETVAL=0

start() {
  if [ -e /var/run/captagent.pid ]; then
        echo "[FAILED] Captagent is already running with PID: `cat /var/run/captagent.pid`"
  else      
        echo -n "Starting $prog: "
        # there is something at end of this output which is needed to
        # report proper [ OK ] status in CentOS scripts
        start-stop-daemon --start --quiet --pidfile $pidfile --exec $cap -- $OPTIONS || echo "Failed"
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && touch $lockfile
  fi
}

stop() {
	echo -n "Stopping $prog: "
	#killproc $cap
	if [ -e /var/run/captagent.pid ]; then
		start-stop-daemon --oknodo --stop --quiet --pidfile $pidfile --exec $cap || echo "Failed"
		RETVAL=$?
		echo
		[ $RETVAL = 0 ] && rm -f $lockfile $pidfile
	else
		RETVAL=1
		echo
		[ $RETVAL = 1 ] && rm -f $lockfile $pidfile
		echo "[FAILED] Captagent is not running"
                
	fi
}


status() {
	RETVAL=0
        if [ -e /var/run/captagent.pid ]; then
                        echo "[OK] Captagent is running with PID: `cat /var/run/captagent.pid`"
        else
                        echo "[FAILED] Captagent is not running"
                        RETVAL=1
        fi
        
	return $RETVAL
}




[ -z "$CONFIG" ]  && CONFIG=/usr/local/etc/captagent/captagent.xml

OPTIONS="-f $CONFIG"

# See how we were called.
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	status)
		status
		;;
	restart)
		stop
		start
		;;
	condrestart)
		if [ -f $pidfile ] ; then
			stop
			start
		fi
		;;
	*)
		echo $"Usage: $prog {start|stop|restart|condrestart|status|help}"
		exit 1
esac

exit $RETVAL
